package com.gc.service.impl;

import com.gc.entity.*;
import com.gc.repository.*;
import com.gc.repository.impl.*;
import com.gc.service.CheckService;
import com.gc.vo.CheckShow;

import java.util.ArrayList;
import java.util.List;

public class CheckServiceImpl implements CheckService {
    CheckRepository checkRepository = new CheckRepositoryImpl();
    CourseRepository courseRepository = new CourseRepositoryImpl();
    TextBookRepository textBookRepository = new TextBookRepositoryImpl();
    TeachClassRepository teachClassRepository = new TeachClassRepositoryImpl();
    CcsRepository ccsRepository = new CcsRepositoryImpl();
    StudentRepository studentRepository = new StudentRepositoryImpl();
    StuCheckRepository stuCheckRepository = new StuCheckRepositoryImpl();
    @Override
    public boolean Add(Check check) {
        return checkRepository.Add(check);
    }

    @Override
    public List<CheckShow> FindAll() {
        List<Check> checks = checkRepository.FindAll();
        List<CheckShow> res = new ArrayList<>();
        for(Check i : checks) {
            Course course = courseRepository.FindByCourseId(i.getCid());
            System.out.println("courseid: " + i.getCid());
            System.out.println("course: " + course);
            TextBook textBook = textBookRepository.FindByBookId(i.getBookid());
            System.out.println("bookid: " + i.getBookid());
            System.out.println("book: " + textBook);
            res.add(new CheckShow(
                    i.getId(),
                    i.getCheckid(),
                    i.getBookid(),
                    textBook.getName(),
                    i.getCid(),
                    course.getName(),
                    i.getStage(),
                    textBook.getPrice(),
                    i.getStarttime(),
                    i.getEndtime()
            ));
        }
        return res;
    }

    @Override
    public boolean DelById(Long id) {
        return checkRepository.DelById(id);
    }

    @Override
    public CheckShow FindById(Long id) {
        Check check = checkRepository.FindById(id);
        Course course = courseRepository.FindByCourseId(check.getCid());

        TextBook textBook = textBookRepository.FindByBookId(check.getBookid());
        return new CheckShow(
                check.getId(),
                check.getCheckid(),
                check.getBookid(),
                textBook.getName(),
                check.getCid(),
                course.getName(),
                check.getStage(),
                textBook.getPrice(),
                check.getStarttime(),
                check.getEndtime()
        );
    }

    @Override
    public boolean UpdateById(Check check) {
        return checkRepository.UpdateById(check);
    }

    @Override
    public CheckShow FindByCheckId(String checkid) {
        Check check = checkRepository.FindByCheckId(checkid);
        Course course = courseRepository.FindByCourseId(check.getCid());

        TextBook textBook = textBookRepository.FindByBookId(check.getBookid());
        return new CheckShow(
                check.getId(),
                check.getCheckid(),
                check.getBookid(),
                textBook.getName(),
                check.getCid(),
                course.getName(),
                check.getStage(),
                textBook.getPrice(),
                check.getStarttime(),
                check.getEndtime()
        );
    }

    /**
     * 根据审核表给该课程的所有学生创建审核表
     * @param id
     * @return
     */
    @Override
    public boolean checkFor(Long id) {
        Check check = checkRepository.FindById(id);
        List<TeachClass> teachClasses = teachClassRepository.FindByCourseId(check.getCid());
        for(TeachClass i : teachClasses) {
            List<Ccs> byClassId = ccsRepository.findByClassId(i.getClassid());
            for(Ccs j : byClassId) {
                Student student = studentRepository.FindByStudentId(j.getSid());
                stuCheckRepository.Add(new StuCheck(
                        null,
                        student.getSid(),
                        check.getCheckid(),
                        0
                ));
            }
        }
        return false;
    }

}
